package controllers

import (
	//	"encoding/json"
	//	"errors"
	"gitee.com/lzqysoft/ilive/models"
	"strconv"
	//	"strings"
	"time"

	"github.com/astaxie/beego"
	//	"fmt"
	"gitee.com/lzqysoft/ilive/libs"
	//	"gitee.com/lzqysoft/ilive/common/log"
	//	"os"
	//	"path"
	"runtime"
)

var ostype = runtime.GOOS

// oprations for Userprof
type UserprofController struct {
	CommonController
}

func (c *UserprofController) URLMapping() {
	//	c.Mapping("Post", c.Post)
	//	c.Mapping("GetOne", c.GetOne)
	c.Mapping("PhotoCancel", c.PhotoCancel)
	c.Mapping("Update", c.Update)

	//	c.Mapping("PhotoUpload", c.PhotoUpload)
}

// @Title Update
// @Description update the Userprof
// @Param	id		path 	string	true		"The id you want to update"
// @Param	body		body 	models.Userprof	true		"body for Userprof content"
// @Success 200 {object} models.Userprof
// @Failure 403 :id is not int
// @router /Update [post]
func (c *UserprofController) Update() {
	user := c.user
	rdata, err := c.AesDecode(c.GetString("tdata"))
	if err != nil {
		c.RetError(ErrToken)
	}
	sdata, _ := rdata.(map[string]interface{})
	sid := sdata["Id"].(string)
	id, _ := strconv.ParseInt(sid, 10, 64)
	v, err := models.GetUserprofById(id)
	if err != nil {
		c.RetError(ErrInputData)
	}
	v.Email = sdata["Email"].(string)
	v.Expert = sdata["Expert"].(string)
	v.Job = sdata["Job"].(string)
	v.Phone = sdata["Phone"].(string)
	//	photo := sdata["Photo"].(string)
	//	fid, _, err := libs.SetFile(photo)
	//	if err != nil {
	//		c.RetError(ErrFds)
	//	}
	if len(v.Photo) > 0 {
		err = libs.DeleteFile(v.Photo)
		if err != nil {
			c.RetError(ErrFds)
		}
	}
	v.Photo = sdata["Photo"].(string)
	v.Title = sdata["Title"].(string)
	v.Remark = sdata["Remark"].(string)
	sex, _ := sdata["Type"].(string)
	v.Sex, _ = strconv.ParseInt(sex, 10, 64)
	v.Updater = user.Name
	v.Utime = time.Now()
	beego.Debug(v.Photo)
	err = models.UpdateUserprofById(v)
	if err != nil {
		c.RetError(ErrDatabase)

	}
	beego.Debug("修改成功")
	c.Dlog("修改了用户属性", v.Id)
	c.RetSuccess(v.Id)
}

// @Title PhotoCancel
// @Description PhotoCancel取消上传文件
// @Success PhotoCancel success!
// @Failure
// @router /PhotoCancel [post]
func (c *UserprofController) PhotoCancel() {
	user := c.user
	beego.Debug(user)
	rdata, err := c.AesDecode(c.GetString("tdata"))
	if err != nil {
		c.RetError(ErrToken)
	}
	sdata, _ := rdata.(map[string]interface{})
	url := sdata["Url"].(string)
	//	var path string
	//	//	os.Remove("/data/root/" + Url)
	//	if ostype == "windows" {
	//		//		url = path
	//		path = /*"c:\\tgo\\src\\gitee.com/lzqysoft/ilive\\" +*/ url
	//	} else if ostype == "linux" {
	//		path = "/data/root/" + url
	//	}

	//	beego.Debug(path)
	//	os.Remove(url)
	libs.DeleteFile(url)
	c.RetSuccess(url)

	//	c.RetSuccess(v)

}
